package com.share.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.share.domain.MySql.Region;
import com.share.service.RegionService;
import com.share.mapper.RegionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 肆
 * @description 针对表【region(地区信息表)】的数据库操作Service实现
 * @createDate 2025-03-25 15:09:19
 */
@Service
public class RegionServiceImpl extends ServiceImpl<RegionMapper, Region> implements RegionService {

    @Autowired
    RegionMapper regionMapper;

    /**
     * 根据上级code获取下级数据列表
     *
     * @param parentCode
     * @return
     */
    @Override
    public List<Region> treeSelect(String parentCode) {
        List<Region> regionList = regionMapper.selectList(new LambdaQueryWrapper<Region>().eq(Region::getParentCode, parentCode));
        if (!CollectionUtils.isEmpty(regionList)) {
            regionList.forEach(item -> {
                Long count = regionMapper.selectCount(new LambdaQueryWrapper<Region>().eq(Region::getParentCode, item.getCode()));
                item.setHasChildren(count > 0);
            });
        }
        return regionList;
    }


    /**
     * 根据code获取名称
     *
     * @param code
     * @return
     */
    @Override
    public String getNameByCode(String code) {
        if (StringUtils.isEmpty(code)) {
            // code 为空或空字符串时执行的操作
            return "";
        }

        Region region = regionMapper.selectOne(new LambdaQueryWrapper<Region>().eq(Region::getCode, code).select(Region::getName));
        if (region == null) {
            return "";
        }

        return region.getName();
    }

}




